
if(util==null) var util = {};

if (document.getElementById) {
  util.byId = function() {
    var elements = new Array();
    for (var i = 0; i < arguments.length; i++) {
      var element = arguments[i];
      if (typeof element == 'string') {
        element = document.getElementById(element);
      }
      if (arguments.length == 1) {
        return element;
      }
      elements.push(element);
    }
    return elements;
  };
}
else if (document.all) {
  util.byId = function() {
    var elements = new Array();
    for (var i = 0; i < arguments.length; i++) {
      var element = arguments[i];
      if (typeof element == 'string') {
        element = document.all[element];
      }
      if (arguments.length == 1) {
        return element;
      }
      elements.push(element);
    }
    return elements;
  };
}

var $$;
if (!$$) {
  $$ = util.byId;
}

util.removeAllRows = function(ele, options) {
  ele = util._getElementById(ele, "removeAllRows()");
  if (ele == null) return;
  if (!options) options = {};
  if (!options.filter) options.filter = function() { return true; };
  if (!util._isHTMLElement(ele, ["table", "tbody", "thead", "tfoot"])) {
    return;
  }
  var child = ele.firstChild;
  var next;
  while (child != null) {
    next = child.nextSibling;
    if (options.filter(child)) {
      ele.removeChild(child);
    }
    child = next;
  }
}

util._isHTMLElement = function(ele, nodeName) {
  if (ele == null || typeof ele != "object" || ele.nodeName == null) {
    return false;
  }
  if (nodeName != null) {
    var test = ele.nodeName.toLowerCase();
    if (typeof nodeName == "string") {
      return test == nodeName.toLowerCase();
    }
    if (util._isArray(nodeName)) {
      var match = false;
      for (var i = 0; i < nodeName.length && !match; i++) {
        if (test == nodeName[i].toLowerCase()) {
          match =  true;
        }
      }
      return match;
    }
    return false;
  }
  return true;
}

util.cloneNode = function(ele, options) {
  ele = util._getElementById(ele, "cloneNode()");
  if (ele == null) return null;
  if (options == null) options = {};
  var clone = ele.cloneNode(true);
  if (options.idPrefix || options.idSuffix) {
    util._updateIds(clone, options);
  }
  else {
    util._removeIds(clone);
  }
  ele.parentNode.insertBefore(clone, ele);
  return clone;
}

util._updateIds = function(ele, options) {
  if (options == null) options = {};
  if (ele.id) {
    ele.setAttribute("id", (options.idPrefix || "") + ele.id + (options.idSuffix || ""));
  }
  var children = ele.childNodes;
  for (var i = 0; i < children.length; i++) {
    var child = children.item(i);
    if (child.nodeType == 1 /*Node.ELEMENT_NODE*/) {
      util._updateIds(child, options);
    }
  }
}

util._removeIds = function(ele) {
  if (ele.id) ele.removeAttribute("id");
  var children = ele.childNodes;
  for (var i = 0; i < children.length; i++) {
    var child = children.item(i);
    if (child.nodeType == 1 /*Node.ELEMENT_NODE*/) {
      util._removeIds(child);
    }
  }
}

util._getElementById = function(ele, source) {
  var orig = ele;
  ele = util.byId(ele);
  if (ele == null) {
    return ele;
  }
  return ele;
}

util._isArray = function(data) {
  return (data && data instanceof Array);
}

util.convertDateVN = function (vdate){
    if(vdate==null||vdate=='') return '';
    var day = vdate.getDate();
    if(day.length<2) day = "0"+day;
    var month = vdate.getMonth()+1;
    if (month.length<2) month = "0"+month;
    var year = vdate.getFullYear();
    day = day+"/"+month+"/"+year;
    return (day);
}

//Cong them  cac ngay thu 7, CN vao bien dayNumber neu trong khoang thoi gian tu startDate + daynumber co' thu 7, CN
util.checkWeekend = function (startDate, dayNumber){
    var datePointer = startDate;
    for(i=1; i<=dayNumber; i++)
    {
        datePointer.setDate(datePointer.getDate() + 1);
        if((datePointer.getDay()==6)||(datePointer.getDay()==0)){
            dayNumber++;
        }   
    }
    return (datePointer);
}

util.getChecked = function (parentId, type, exceptionId){
    var result = '';
    var grid = document.getElementById(parentId);
    var allc = grid.getElementsByTagName("input");
    for (i=0;i<allc.length;i++){
        if (allc[i].type==type&&allc[i].checked&&allc[i].id!=exceptionId){
            result += (result=='')?(allc[i].value):(','+allc[i].value);	
        }
    }
    return result;
}

<!-- ALL TRIM FUNCTION-->
/*
 PROTOTYPE  : Trim(s)
 INPUT PARAM: STRING
 PROCESS    : Cat bo cac ky tu white space o 2 dau` chuoi
 OUTPUT     : CHUOI SAU KHI CAT
 */
util.Trim = function (s){
    if(s!=null&&s.length>0){
        while ((s.substring(0,1) == ' ') || (s.substring(0,1) == '\n') || (s.substring(0,1) == '\r')){
            s = s.substring(1,s.length);
        }
        while ((s.substring(s.length-1,s.length) == ' ') || (s.substring(s.length-1,s.length) == '\n') || (s.substring(s.length-1,s.length) == '\r')){
            s = s.substring(0,s.length-1);
        }
    }
    return s;
}

util.isDate = function (strDate) {
    var re = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{2,4})$/;
    if (!re.test(strDate)) return false;
    strDate = strDate.split('/');
    if(strDate.length==3){
        year = parseInt(strDate[2]);
        while(strDate[1].substr(0,1)=='0' && strDate[1].length>1) strDate[1] = strDate[1].substr(1);
        month = parseInt(strDate[1]);
        while(strDate[0].substr(0,1)=='0' && strDate[0].length>1) strDate[0] = strDate[0].substr(1);
        day = parseInt(strDate[0]);
        // month argument must be in the range 1 - 12
        month = month - 1; // javascript month range : 0- 11
        var tempDate = new Date(year,month,day);
        if ( (tempDate.getFullYear() == year) &&
            (month == tempDate.getMonth()) &&
            (day == tempDate.getDate()) )
            return true;
    }
    return false;
}
util.setCookie = function(c_name,value,expiredays){
    var exdate=new Date();
    exdate.setDate(exdate.getDate()+expiredays);
    document.cookie=c_name+ "=" +escape(value)+
    ((expiredays==null) ? "" : ";expires="+exdate.toUTCString());
}
util.getCookie = function(c_name){
    if (document.cookie.length>0){
        c_start=document.cookie.indexOf(c_name + "=");
        if (c_start!=-1){
            c_start=c_start + c_name.length+1;
            c_end=document.cookie.indexOf(";",c_start);
            if (c_end==-1) c_end=document.cookie.length;
            return unescape(document.cookie.substring(c_start,c_end));
        }
    }
    return "";
}

/* dvchinh */

    var ascii = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.";
//    var spec="~`!@#$%^*+=|{}[];:'\"<>/"; //ky tu dac biet   
//    var specFull="~`!@#$%^*+_-?.,=|{}[];:'\"<>/"; //ky tu dac biet   
//    var specNNKD="~`!@#$%^*+=|{}[]:'\"<>/"; //ky tu dac biet 
//    var specCharLittle="~`!@#$%^*+=|{};:'\"<>?"; //ky tu dac biet   
    var spec="`'"; //ky tu dac biet   
    var specFull="`'"; //ky tu dac biet   
    var specNNKD="`'"; //ky tu dac biet 
    var specCharLittle="`'"; //ky tu dac biet   
    var date = "/" + number;
    var number = "0123456789";
    var curency = number + ".,";
    var passchar = ascii + spec + "&-_";
    var phone = " -().eEx" + number;
    var pattern = ".";
    var nonemail =   "~`!#$%^*()+=|\\{}[];:'\"<>,/?";
    var nonaddress = "~`!@#$%^*()+=|\\{}[];:'\"<>?";
    var nonwebsite = "~`!@#$%^*()+=|\\{}[];'\"<>?";
    var nonchar = spec + "&-_" + number; 
    var variable = ascii + "&-_";
    var hoten = nonemail + number + "&-_";

    /*
        @author: dvchinh
        Ham kiem tra gia tri nhap lieu hop le
        Tra ve xau loi neu chua hop le, tra ve xau rong neu du lieu hop le
        Input:
            - Name: ten doi tuong
            - greater: do dai toi da cua doi tuong
            - lower: do dai toi thieu
            - argSpecChar: chuoi du lieu so sanh
            - except: 0: chi nhap cac ky tu trong chuoi argSpecChar, 1: khong nhap cac ky tu trong chuoi argSpecChar
            - isUpCase: 0: khong chuyen doi tuong thanh chu hoa, 1: chuyen doi tuong thanh chu hoa
            - messpart: cau thong bao khi co loi
        Output:Tra ve xau loi neu chua hop le, tra ve xau rong neu du lieu hop le
    */
    function isValid(Name,greater,lower,argSpecChar,except,isUpCase,messpart){
        var strMsg = "";
        var ctrName = document.getElementById(Name).value;
        try{
        ctrName = util.Trim(ctrName);
        }catch (e) {};
        if (isUpCase==1){
            var str = ctrName.toUpperCase();
            ctrName = str;
            document.getElementById(Name).innerText = str;
        }
        if (lower==-1){
		    if (ctrName.length>0)
			    if (ctrName.length>greater)
				    strMsg += "\n - " + messpart + " ph\u1EA3i nh\u1ECF h\u01A1n " + (greater+1) + " k\u00FD t\u1EF1!";
        }else{
            if (ctrName.length==0){
			    strMsg += "\n - Xin nh\u1EADp v\u00E0o " + messpart + "!";
            }else if (ctrName.length>greater || ctrName.length<lower){
                strMsg += "\n - " + messpart + " ph\u1EA3i l\u1EDBn h\u01A1n " + (lower-1) + " k\u00FD t\u1EF1 v\u00E0 nh\u1ECF h\u01A1n " + (greater+1) + " k\u00FD t\u1EF1!";
            }
        }
        for (var i=0; i<ctrName.length; i++) {
            temp = "" + ctrName.substring(i, i+1);
            if (except==1){
                if (argSpecChar.indexOf(temp) != "-1"){
                    strMsg += "\n - " + messpart +  " kh\u00F4ng \u0111\u01B0\u1EE3c nh\u1EADp c\u00E1c k\u00FD t\u1EF1: \n " + argSpecChar;
                    break;
                }
            }else{
                if (argSpecChar.indexOf(temp) == "-1"){
                    strMsg += "\n - " + messpart +  " ch\u1EC9 \u0111\u01B0\u1EE3c nh\u1EADp c\u00E1c k\u00FD t\u1EF1: \n" + argSpecChar;
                    break;
                }
            }
        }
        return strMsg;
    }
    
    function isValidEx(Name,greater,lower,argSpecChar,except,isUpCase,messpart){
        var strMsg = "";
        var ctrName = Name; //document.getElementById(Name).value;
        try{
        ctrName = util.Trim(ctrName);
        }catch (e) {};
        if (isUpCase==1){
            var str = ctrName.toUpperCase();
            ctrName = str;
            document.getElementById(Name).innerText = str;
        }
        if (lower==-1){
		    if (ctrName.length>0)
			    if (ctrName.length>greater)
				    strMsg += "\n - " + messpart + " ph\u1EA3i nh\u1ECF h\u01A1n " + (greater+1) + " k\u00FD t\u1EF1!";
        }else{
            if (ctrName.length==0){
			    strMsg += "\n - Xin nh\u1EADp v\u00E0o " + messpart + "!";
            }else if (ctrName.length>greater || ctrName.length<lower){
                strMsg += "\n - " + messpart + " ph\u1EA3i l\u1EDBn h\u01A1n " + (lower-1) + " k\u00FD t\u1EF1 v\u00E0 nh\u1ECF h\u01A1n " + (greater+1) + " k\u00FD t\u1EF1!";
            }
        }
        for (var i=0; i<ctrName.length; i++) {
            temp = "" + ctrName.substring(i, i+1);
            if (except==1){
                if (argSpecChar.indexOf(temp) != "-1"){
                    strMsg += "\n - " + messpart +  " kh\u00F4ng \u0111\u01B0\u1EE3c nh\u1EADp c\u00E1c k\u00FD t\u1EF1: \n " + argSpecChar;
                    break;
                }
            }else{
                if (argSpecChar.indexOf(temp) == "-1"){
                    strMsg += "\n - " + messpart +  " ch\u1EC9 \u0111\u01B0\u1EE3c nh\u1EADp c\u00E1c k\u00FD t\u1EF1: \n" + argSpecChar;
                    break;
                }
            }
        }
        return strMsg;
    }
    /*---get Id & Text of treeview----*/
    function GetNodeValue(node)
    {
        //node value
        var nodeValue = "";
        var nodePath = node.href.substring(node.href.indexOf(",")+2,node.href.length-2);
        var nodeValues = nodePath.split("\\");
        if(nodeValues.length > 1)
            nodeValue = nodeValues[nodeValues.length - 1];
        else
            nodeValue = nodeValues[0].substr(1); 
        return nodeValue;
    }   
    function GetNodeText(node,mEvent)
    {
        var nodeText = "";
        var nav = navigator.userAgent.toLowerCase();	
        if (nav.indexOf('msie') > -1)//IE
        	nodeText = node.innerText;
        else if (mEvent.target)//Netscape and Firefox	
            nodeText = node.text;
        return nodeText;        
    } 
    //check type image
    function isImage(inputVal){
        if (inputVal=="")
            return true;
        else    
            return (/.(gif|jpg|jpeg|jpe|png|bmp|tif?g)$/i.test(inputVal));    
    }
    //check type document
    function isDoc(inputVal){
        if (inputVal=="")
            return true;
        else
            return (/.(doc|pdf|xls|ppt?g)$/i.test(inputVal));    
    }
    //get year
    function getYears(dt){
        if(dt.getFullYear)
            return dt.getFullYear();
        else
            return dt.getYear() + 1900; 
        var _year;      
    }
    function isNumber(val){
        var kt = /^([1-9]{1}[0-9]{0,4})$/;
        if (!kt.test(val))
        {   
	       return false;
        } 
        return true;
    }    
 